import Flutter
import Photos

public class IosPhotoLibraryPermissionPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "ios_photo_library_permission", binaryMessenger: registrar.messenger())
    let instance = IosPhotoLibraryPermissionPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      switch call.method {
      case "getPhotoLibraryPermission":
        result(PHPhotoLibrary.authorizationStatus().rawValue)
      case "requestPhotoLibraryPermission":
      PHPhotoLibrary.requestAuthorization { status in
          if status == .authorized {
              result(true)
          } else {
              result(false)
          }
      }
      default:
        result(FlutterMethodNotImplemented)
      }
    }
}
